// $Id: buffer.h 64 2011-04-23 11:35:44Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2010 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef BUFFER_H_
#define BUFFER_H_

#include <sys/queue.h>

#include "nio/buffer.h"

struct niobuffer_item {
	struct niobuffer *buffer;
	TAILQ_ENTRY(niobuffer_item) entry;
};

TAILQ_HEAD(niobuffer_list, niobuffer_item);

int niobuffer_list_add(struct niobuffer_list *list, struct niobuffer *buffer);

void niobuffer_list_clear(struct niobuffer_list *list);

enum niobuffer_type {
	NIO_BUFFER_UNKNOWN,
	NIO_BUFFER_DIRECT,
	NIO_BUFFER_ROTATED,
	NIO_BUFFER_CLONED,
	NIO_BUFFER_WRAPPER,
};

struct niobuffer {
	enum niobuffer_type type;
	union {
		struct niobuffers *factory;
		struct niobuffer *buffer;
	} source;
	unsigned char *data;
	size_t capacity;
	size_t limit;
	size_t position;
	size_t mark;
	int released;
	niobuffer_cb cb;
	void *cbarg;
	struct niobuffer_list cloned;
	struct niobuffer_list wrapped;
	struct niobuffer_item *current;
	size_t num_wrapped;
	struct niobuffer *wrapper;
};

#endif /* BUFFER_H_ */
